//
//  CustomAlerView.m
//  BigJoy
//
//  Created by Rowling on 2017/6/20.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "CustomAlerView.h"

@interface CustomAlerView()
@property (nonatomic,strong)UIView * centerView;
@end

@implementation CustomAlerView

-(UIView *)centerView {
    if (_centerView == nil) {
        _centerView = [[UIView alloc]init];
    }
    return _centerView;
}

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

-(void)setUI {
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
}

-(void)setUIWithBlock:(void(^)(void))left andLeftStr:(NSString *)leftStr andContent:(NSString *)str {
    CGFloat centerViewWidth = SCREEN_WIDTH *  3/4;
    CGFloat contentheigh = [XMSuperHelper LabelHeight:str Width:centerViewWidth - 30 Font:[UIFont systemFontOfSize:14]] + 15+20;
    [self addSubview:self.centerView];
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.centerY.equalTo(self).multipliedBy(0.9);
        make.height.equalTo(@(15+15+contentheigh+40));
        make.width.equalTo(@(centerViewWidth));
    }];
    self.centerView.layer.cornerRadius = 8;
    self.centerView.layer.masksToBounds = YES;
    self.centerView.backgroundColor = [UIColor whiteColor];
    
    // tishilable
    UILabel * tishiLable = [[UILabel alloc]init];
    tishiLable.text = @"提示";
    [self.centerView addSubview:tishiLable];
    [tishiLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.centerView).offset(15);
        make.centerX.equalTo(self.centerView);
    }];
    //contentLable
    UILabel * contentLable = [[UILabel alloc]init];
    contentLable.text = str;
    contentLable.font = [UIFont systemFontOfSize:12];
    contentLable.numberOfLines = 0;
    contentLable.textColor = [UIColor colorForHex:@"666666"];
    [self.centerView addSubview:contentLable];
    [contentLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tishiLable.mas_bottom).offset(15);
        make.left.equalTo(self.centerView).offset(15);
        make.right.equalTo(self.centerView).offset(-15);
        
    }];
    //botmomButton;
    UIButton * cancelButton = [[UIButton alloc]init];
    [self.centerView addSubview:cancelButton];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:13.5];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor colorForHex:@"666666"] forState:UIControlStateNormal];
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.centerView);
        make.width.equalTo(@(centerViewWidth / 2 - 1));
        make.height.equalTo(@40);
    }];
    @weakify(self);
    [[cancelButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self removeFromSuperview];
    }];
    //centerlineView
    UIView * centerLineView = [[UIView alloc]init];
    [self.centerView addSubview:centerLineView];
    [centerLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cancelButton.mas_right);
        make.bottom.equalTo(self.centerView);
        make.width.equalTo(@1);
        make.height.equalTo(cancelButton);
    }];
    centerLineView.backgroundColor = XMColor(240, 240, 242);
    // okbutton
    UIButton * okButton = [[UIButton alloc]init];
    [self.centerView addSubview:okButton];
    [okButton setTitle:leftStr forState:UIControlStateNormal];
    [okButton setTitleColor:XMColor(245, 70, 70) forState:UIControlStateNormal];
    okButton.titleLabel.font = [UIFont systemFontOfSize:13.5];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(centerLineView.mas_right);
        make.bottom.equalTo(self.centerView);
        make.width.equalTo(@(centerViewWidth / 2));
        make.height.equalTo(@40);
    }];

    [[okButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        if (left) {
            left();
        }
        [self removeFromSuperview];
    }];
    
    //henlineView
    UIView * hengLineView = [[UIView alloc]init];
    [self.centerView addSubview:hengLineView];
    [hengLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.centerView);
        make.height.equalTo(@1);
        make.bottom.equalTo(cancelButton.mas_top);
    }];
    hengLineView.backgroundColor = XMColor(240, 240, 242);
}

-(void)singleButtonAlert {
    UIButton * okButton = [[UIButton alloc]init];
    [self.centerView addSubview:okButton];
    [okButton setTitle:@"确定" forState:UIControlStateNormal];
    [okButton setTitleColor:XMColor(245, 70, 70) forState:UIControlStateNormal];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.centerView);
        make.height.equalTo(@40);
    }];
    okButton.backgroundColor = self.centerView.backgroundColor;
    @weakify(self);
    [[okButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        [self removeFromSuperview];
    }];
}

+(void)showMessageWith:(NSString *)str leftStr:(NSString *)leftStr leftAction:(void(^)(void))block {
    CustomAlerView * view = [[CustomAlerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [view setUIWithBlock:block andLeftStr:leftStr andContent:str];
    [XMKEYWINDOWXM addSubview:view];
    [view layoutIfNeeded];
}

+(void)showAlertMessage:(NSString *)str {
    CustomAlerView * view = [[CustomAlerView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [view setUIWithBlock:nil andLeftStr:@"" andContent:str];
    [view singleButtonAlert];
    [XMKEYWINDOWXM addSubview:view];
    [view layoutIfNeeded];
}

-(void)dealloc {
    NSLog(@"CustomAlerViewDealloc");
}

@end
